package org.bigjava.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bigjava.travel.bean.Car;
import org.bigjava.travel.dao.CarDao;
import org.bigjava.travel.service.CarService;
import org.bigjava.travel.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 车票业务逻辑层实现类
 */
@Service
public class CarServiceImpl implements CarService {

    @Resource
    CarDao carDao;

    @Override
    public long count() throws Exception {
        return carDao.count();
    }

    @Override
    public Car findById(Integer id) throws Exception {
        return carDao.findById(id);
    }

    @Override
    public List<Car> findList() throws Exception {
        return carDao.findList();
    }

    @Override
    public void save(Car car) throws Exception {
        carDao.save(car);
    }

    @Override
    public void update(Car car) throws Exception {
        carDao.update(car);
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        carDao.deleteById(id);
    }

    @Override
    public List<Car> findByPage(int currentPage, int pageSize, String query) {
        List<Car> list = new ArrayList<Car>();
        PageHelper.startPage(currentPage, pageSize);
        if (Tools.notEmpty(query)) {
            list = carDao.findListByQuery("%" + query + "%");
        } else {
            list = carDao.findList();
        }
        PageInfo<Car> pageInfo=new PageInfo<Car>(list);
        return pageInfo.getList();
    }

    @Override
    public List<Car> findByPage(int currentPage, int pageSize) {
        List<Car> list = new ArrayList<Car>();
        PageHelper.startPage(currentPage, pageSize);
        list = carDao.indexList();
        PageInfo<Car> pageInfo=new PageInfo<Car>(list);
        return pageInfo.getList();
    }

    @Override
    public long state1count() throws Exception {
        return carDao.state1count();
    }

    @Override
    public long state2count() throws Exception {
        return carDao.state2count();
    }

    @Override
    public long state0count() throws Exception {
        return carDao.state0count();
    }

    @Override
    public long type1count() throws Exception {
        return carDao.type1count();
    }

    @Override
    public long type2count() throws Exception {
        return carDao.type2count();
    }
}
